import { EventMgr, Event_Name } from "../framework/EventMgr";

export default class UserMgr {
    private static m_instance: UserMgr

    private constructor () {}
  
    private m_giftInfo = null;
    static get Instance()
    {
        if (!UserMgr.m_instance) {
            UserMgr.m_instance = new UserMgr();
        }
        return UserMgr.m_instance
    }

    private m_userInfo = null;

    setInfo (userInfo) {
        this.m_userInfo = userInfo;
    }
    
    getUid () {
        return this.getUserInfo("uid")
    }

    getToken () {
        return this.getUserInfo("token")
    }

    getUserInfo(key?:string) {
        if (this.m_userInfo) {
            if (key) {
                return this.m_userInfo[key];
            } else {
                return this.m_userInfo;
            }
        }else{
            return null;
        }
    }

    // 直接覆盖 userInfo 完整结构
    setUserInfo (userInfo) {
        if (this.m_userInfo && userInfo) {
            this.m_userInfo = userInfo;
        }
    }

    // 只更新部分数据
    updateUserInfo (userInfo) {
        if (this.m_userInfo && userInfo) {
            for (const key in userInfo) {
                // console.log(key);                
                if (userInfo.hasOwnProperty(key)) {
                    this.m_userInfo[key] = userInfo[key];                    
                }
            }
        }
        EventMgr.Instance.emit(Event_Name.USER_INFO_CHANGED);
    }

    // 存储卡包信息
    setGiftInfo (list) {
        if (this.m_giftInfo == null) {
            this.m_giftInfo = {}
        }

        for (const iterator of list) {
            this.m_giftInfo[iterator.gift_type] = iterator;
            if (iterator.status == 2) {
                iterator.finishTime = Math.floor(Date.now() / 1000) + iterator.remainTime;
            }
        }
    }

    // 存储一个或所有卡包信息
    getGiftInfo (type?) {
        if (this.m_giftInfo) {
            if (type) {
                return this.m_giftInfo[type];
            } else {
                return this.m_giftInfo;
            }
        }else{
            return null;
        }
    }

    // 更新一个卡包信息
    updateOneGiftInfo (data) {
        if (data) {
            let info = this.m_giftInfo[data.gift_type];
            if (info && data) {
                for (const key in data) {
                    // console.log(key);                
                    if (data.hasOwnProperty(key)) {
                        info[key] = data[key];
                    }
                }
                if (info.status == 2) {
                    info.finishTime = Math.floor(Date.now() / 1000) + info.remainTime;
                }
            }
            EventMgr.Instance.emit(Event_Name.GIFT_ONE_UPDATE, data.gift_type);
        }        
    }

    updateAllGiftInfo (list) {
        this.setGiftInfo(list);
        EventMgr.Instance.emit(Event_Name.GIFT_ALL_UPDATE);
    }

    canGiftSign () {
        let ret = true;
        for (const key in this.m_giftInfo) {
            if (this.m_giftInfo.hasOwnProperty(key)) {
                const info = this.m_giftInfo[key];
                if (info.status == 2) {
                    ret = false;
                    info.finishTime = Math.floor(Date.now() / 1000) + info.remainTime;
                }
            }
        }
        return ret;
    }
}
